package cn.gwm.flink.streaming.strategy.multidimension;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import cn.gwm.flink.streaming.strategy.vehiclepredict.DwsStrConstant;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @author : GW00280745
 * @date : 2023/1/9 14:05
 * @description :
 * 维度的排列
 *  地点
 *  电流
 *  温度
 *  soc
 *  bms_heatreq
 *  acopensts
 *
 */
public class DimensionOrder implements Serializable {

    private String dcStr = " to_number(\"bms_dcchrgconnect\")=1 ";
    public StringBuilder dcDim(StringBuilder stringBuilder){
        stringBuilder.append(dcStr);
        return stringBuilder;
    }
    private String chrglogicnum = " to_number(\"chrglogicnum\")=1 ";
    public StringBuilder chrgNumDim(StringBuilder stringBuilder){
        stringBuilder.append(chrglogicnum);
        return stringBuilder;
    }
    private String deviceIdStr = " \"deviceid\"=\'";
    private String vidStr = " substr(id,0,17) =\'";
    private String vidStrNe = " substr(id,0,17) !=\'";
    private String deviceIdStrNe = " \"deviceid\"!=\'";
    private String quotationStr ="\' ";
    public StringBuilder deviceIdDim(StringBuilder stringBuilder,Object vin,Boolean isEqual){
        if (isEqual){
            stringBuilder.append(deviceIdStr).append(vin).append(quotationStr);
        }else {
            stringBuilder.append(deviceIdStrNe).append(vin).append(quotationStr);
        }
        return stringBuilder;
    }

    public StringBuilder deviceIdDim2(StringBuilder stringBuilder,Object vin,Boolean isEqual){
        if (isEqual){
            stringBuilder.append(vidStr).append(vin).append(quotationStr);
        }else {
            stringBuilder.append(vidStrNe).append(vin).append(quotationStr);
        }
        return stringBuilder;
    }
    private String deviceIdLtStr = " \"deviceid\"<=\'";
    private String deviceIdGtStr = " \"deviceid\">=\'";
    private String str0 = "0000";
    private String strz = "~~~~";
    public StringBuilder deviceIdDimLg(StringBuilder stringBuilder,Object vin){
        //LGWEEUA56NE202478
        // "deviceid" >= 'LGWEEUA56NE200000' and "deviceid" <= 'LGWEEUA56NE20~~~~'
        String s = vin.toString();
        String s1 = s.substring(0, s.length() - 4);
        String small = s1.concat(str0);
        String bigger = s1.concat(strz);
        stringBuilder.append(deviceIdGtStr).append(small).append(quotationStr);
        stringBuilder.append(andStr);
        stringBuilder.append(deviceIdLtStr).append(bigger).append(quotationStr);
        return stringBuilder;
    }
    private String andStr = " and ";
    public StringBuilder andOps(StringBuilder stringBuilder){
        stringBuilder.append(andStr);
        return stringBuilder;
    }

    private String itemStr = " \"item_time\"<\'";
    public StringBuilder itemTimeDim(StringBuilder stringBuilder,Object tidVal){
        //查找最近60天内的充电记录
//        stringBuilder.append("  \"item_time\">=\'").append(tidVal-60*24*60*60*1000).append("\'")
        stringBuilder.append(itemStr).append(tidVal).append(quotationStr);
        return stringBuilder;
    }

    private String startTimeStr = " \"startchargetime\"<=\'";
    private String startTimeStr1 = " \"startchargetime\"<\'";
    public StringBuilder startTimeDim(StringBuilder stringBuilder,Object tidVal,boolean firstTime){
        //查找最近60天内的充电记录
        if (firstTime){
            return stringBuilder.append(startTimeStr1).append(tidVal).append(quotationStr);
        }
        stringBuilder.append(startTimeStr).append(tidVal).append(quotationStr);
        return stringBuilder;
    }

    private String currgDimStr = " \"curr_g\"=\'";
    public StringBuilder currgDim(StringBuilder stringBuilder,Object tidVal){
        float i = Math.abs(Float.parseFloat(tidVal.toString()));
        if (i<10f){
            String concat = "-0".concat(String.valueOf((int)i));
            stringBuilder.append(currgDimStr).append(concat).append(quotationStr);
        }else{
            String concat = "-".concat(String.valueOf((int)i));
            stringBuilder.append(currgDimStr).append(concat).append(quotationStr);
        }
        return stringBuilder;
    }
    private String modelDimStr = " \"model_code\"=\'";
    public StringBuilder modelDim(StringBuilder stringBuilder,Object tidVal){
        stringBuilder.append(modelDimStr).append(tidVal).append(quotationStr);
        return stringBuilder;
    }
    private String batterCapDimStr = " \"batter_capcity\"=\'";
    public StringBuilder batterCapDim(StringBuilder stringBuilder,Object tidVal){
        stringBuilder.append(modelDimStr).append(tidVal).append(quotationStr);
        return stringBuilder;
    }
    private String poiidDimStr = " \"poiid\"=\'";
    public StringBuilder poiidDim(StringBuilder stringBuilder,Object poiidObj){
        if (poiidObj == null || StringUtils.isBlank(poiidObj.toString())){
            poiidObj = ChargePredictFields.ins().blankStr9;
        }
        //查找最近60天内的充电记录
        stringBuilder.append(poiidDimStr).append(poiidObj).append(quotationStr);
        return stringBuilder;
    }

    private String sohStr = " and to_number(\"BMS_SOH\")>=";
    public void sohDim(StringBuilder stringBuilder,Object soh){
        float sohF = 100f;
        try {
            sohF = Float.parseFloat(soh.toString());
        }catch (Exception e){}
        stringBuilder.append(sohStr).append(sohF-5f);

    }

    private String socStr = "  to_number(\"bms_innersoc\")>";
    private String socStrGe = "  to_number(\"bms_innersoc\")>=";
    private String socStrLe = " and to_number(\"bms_innersoc\")<=";
    private String socStrGe1 = "  \"bms_innersoc\">=";
    private String socStrLe1 = " and \"bms_innersoc\"<=";
    public StringBuilder socDim3(StringBuilder stringBuilder,Object bmsSoc,float socStep){
        //soc当作字符串比较大小因此需要进行补位 以100.0为基准
        float soc = Math.abs(Float.parseFloat(bmsSoc.toString()));
        stringBuilder.append(socStrGe1).append(quotationStr1).append(socConcat(soc-socStep)).append(quotationStr);
        stringBuilder.append(socStrLe1).append(quotationStr1).append(socConcat(soc+socStep)).append(quotationStr);
        return stringBuilder;
    }
    private String socConcat(float socVal){
        //统一补齐到5位
        String socValStr = String.valueOf(socVal);
        int length = socValStr.length();
        if (length==1){
            return "0000".concat(socValStr);
        }else if (length== DwsStrConstant.ins().num2){
            return "000".concat(socValStr);
        }else if (length==DwsStrConstant.ins().num3){
            return "00".concat(socValStr);
        }else if (length==DwsStrConstant.ins().num4){
            return "0".concat(socValStr);
        }
        return socValStr;
    }
    public StringBuilder socDim2(StringBuilder stringBuilder,Object bmsSoc,float socStep){
        float soc = Math.abs(Float.parseFloat(bmsSoc.toString()));
        stringBuilder.append(socStrGe).append(soc-socStep);
        stringBuilder.append(socStrLe).append(soc+socStep);
        return stringBuilder;
    }
    public StringBuilder socDim(StringBuilder stringBuilder,Object bmsSoc,Boolean endSocFlag){
        float soc = Math.abs(Float.parseFloat(bmsSoc.toString()));
        if (endSocFlag){
            stringBuilder.append(socStr).append(soc);
        }else {
            stringBuilder.append(socStrGe).append(soc-2.5f);
            stringBuilder.append(socStrLe).append(soc+2.5f);
        }
        return stringBuilder;
    }
    public StringBuilder currDimDc(StringBuilder stringBuilder, Object bmsBattcurr, boolean isObc, int stepVal){
        stringBuilder.append("  to_number(\"bms_battcurr_rm\")>=-300.0");
        return stringBuilder;
    }
    private String currStrObcLt = "  \"bms_battcurr_rm\" <=\'";
    private String currStrObcGt = "  \"bms_battcurr_rm\" >=\'";
    private String currStr25 = "  to_number(\"bms_battcurr_rm\")>=-25 ";
    private String currStr1 = " and to_number(\"bms_battcurr_rm\")<=-1 ";
    public StringBuilder currDim(StringBuilder stringBuilder,Object bmsBattcurr,boolean isObc,int stepVal){
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        if (isObc){
            stringBuilder.append(currStr25);
            stringBuilder.append(currStr1);
            return stringBuilder;
        }
        stringBuilder.append("  to_number(\"bms_battcurr_rm\")>=-").append(curr+15);
        if (curr-stepVal<0){
            stringBuilder.append(" and to_number(\"bms_battcurr_rm\")<=0");
        }else {
            stringBuilder.append(" and to_number(\"bms_battcurr_rm\")<=-").append(curr-stepVal);
        }
        return stringBuilder;
    }
    public StringBuilder currDimObc(StringBuilder stringBuilder, Object bmsBattcurr, boolean isObc, int stepVal){
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        if (isObc){
            float v1 = curr - 2;
            float v = curr + 2;
            if (v1<10){
                stringBuilder.append(currStrObcGt).append("-0").append(v1).append(quotationStr);
            }else {
                stringBuilder.append(currStrObcGt).append("-").append(v1).append(quotationStr);
            }
            stringBuilder.append(andStr);
            if (v<10){
                stringBuilder.append(currStrObcLt).append("-0").append(v).append(quotationStr);
            }else {
                stringBuilder.append(currStrObcLt).append("-").append(v).append(quotationStr);
            }
            return stringBuilder;
        }
        stringBuilder.append("  to_number(\"bms_battcurr_rm\")>=-").append(curr+15);
        if (curr-stepVal<0){
            stringBuilder.append(" and to_number(\"bms_battcurr_rm\")<=0");
        }else {
            stringBuilder.append(" and to_number(\"bms_battcurr_rm\")<=-").append(curr-stepVal);
        }
        return stringBuilder;
    }

    public StringBuilder currDimObc1SocV2(StringBuilder stringBuilder, Object bmsBattcurr, int stepVal){
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        float v1 = curr - 2;
        if (v1<10){
            stringBuilder.append(currStrObcGt).append("-0").append(v1).append(quotationStr);
        }else {
            stringBuilder.append(currStrObcGt).append("-").append(v1).append(quotationStr);
        }

        stringBuilder.append(andStr);
        float v = curr + 2;
        if (v<10){
            stringBuilder.append(currStrObcLt).append("-0").append(v).append(quotationStr);
        }else {
            stringBuilder.append(currStrObcLt).append("-").append(v).append(quotationStr);
        }
        return stringBuilder;
    }

    private String mapIdStr = "  \"mapid\"=\'";
    public StringBuilder mapIdDim(StringBuilder stringBuilder,Object acopensts){
        stringBuilder.append(mapIdStr).append(acopensts).append(quotationStr);
        return stringBuilder;
    }
    private String acOpenStr = "  \"acopensts\"=\'";
    public StringBuilder acOpenDim(StringBuilder stringBuilder,Object acopensts){
        if ( acopensts==null|| StringUtils.equalsIgnoreCase(acopensts.toString(),ChargePredictFields.ins().defaultVal)){
            acopensts = ChargePredictFields.ins().zeroStr;
        }
        stringBuilder.append(acOpenStr).append(acopensts).append(quotationStr);
        return stringBuilder;
    }
    private String heatReqStr = "  \"bms_heatreq\"=\'";
    public StringBuilder heatReqDim(StringBuilder stringBuilder,Object bmsHeatreq){
        if ( bmsHeatreq==null|| StringUtils.equalsIgnoreCase(bmsHeatreq.toString(), ChargePredictFields.ins().defaultVal)){
            bmsHeatreq = ChargePredictFields.ins().zeroStr;
        }
        stringBuilder.append(heatReqStr).append(bmsHeatreq).append(quotationStr);
        return stringBuilder;
    }
    public StringBuilder temp5Dim(StringBuilder stringBuilder,Object acambtemp){
        float temp = Math.abs(Float.parseFloat(acambtemp.toString()));
        if (StringUtils.containsIgnoreCase(acambtemp.toString(),kangStr)){
            stringBuilder.append(toNumAcAmbDfNa).append(temp+5f);
        }else {
//            stringBuilder.append(" and to_number(\"ACAmbTemp\")<=").append(temp+5f)
            stringBuilder.append(toNumAcAmbDna).append(temp-5f);
        }
        return stringBuilder;
    }
    private String kangStr = "-";
    private String acAmbLt = "  \"bms_rmc_moduletempmin\" <=";
    private String acAmbGt = "  \"bms_rmc_moduletempmin\" >=";
    private String toNumAcAmb = "  to_number(\"bms_rmc_moduletempmin\")<=";
    private String toNumAcAmbF = "  to_number(\"bms_rmc_moduletempmin\")<=-";
    private String toNumAcAmbDf = " and to_number(\"bms_rmc_moduletempmin\")>=-";
    private String toNumAcAmbD = " and to_number(\"bms_rmc_moduletempmin\")>=";
    private String toNumAcAmbDna = "  to_number(\"bms_rmc_moduletempmin\")>=";
    private String toNumAcAmbDfNa = "  to_number(\"bms_rmc_moduletempmin\")>=-";
    private String startSocOp = "  to_number(\"bms_innersoc\") <= to_number(\"steady_soc\") ";
    public StringBuilder startStageSoc(StringBuilder stringBuilder){
        stringBuilder.append(startSocOp);
        return stringBuilder;
    }
    private String testStr= " to_number(\"end_bms_soc\")>=90.0 and  to_number(\"end_bms_soc\") > to_number(\"bms_innersoc\") ";
    public StringBuilder testS(StringBuilder stringBuilder){
        stringBuilder.append(testStr);
        return stringBuilder;
    }
    private String quotationStr1 ="\'";
    public StringBuilder tempDim2(StringBuilder stringBuilder,Object acambtemp,boolean breakBool){
        int tempVal = Integer.parseInt(acambtemp.toString());
        if (tempVal<0){
            //温度是负数 -2 -8
            int abs = Math.abs(tempVal);
            int i = abs + 3;
            int j = Math.abs(abs - 3);
            if (i>=DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbLt).append(quotationStr1).append("-").append(i).append(quotationStr).append(andStr);
            }else {
                stringBuilder.append(acAmbLt).append(quotationStr1).append("-0").append(i).append(quotationStr).append(andStr);
            }

            if (j>=DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-").append(j).append(quotationStr);
            }else if(j>=0){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-0").append(j).append(quotationStr);
            }else {
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-0").append(Math.abs(j)).append(quotationStr);
            }
        }else if (tempVal>=DwsStrConstant.ins().num0){
            int i = tempVal + 3;
            if (i<DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbLt).append(quotationStr1).append("a0").append(i).append(quotationStr).append(andStr);
            }else {
                stringBuilder.append(acAmbLt).append(quotationStr1).append("a").append(i).append(quotationStr).append(andStr);
            }
            int j = tempVal - 3;
            if (j>=DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("a").append(j).append(quotationStr);
            }else if (j>=0){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("a0").append(j).append(quotationStr);
            }else {
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-0").append(Math.abs(j)).append(quotationStr);
            }
        }
        return stringBuilder;
    }
    public StringBuilder tempDim(StringBuilder stringBuilder,Object acambtemp,boolean breakBool){
        float temp = Math.abs(Float.parseFloat(acambtemp.toString()));
        float plusVal = 10f;
        if (breakBool){
            plusVal=15f;
        }
        if (StringUtils.containsIgnoreCase(acambtemp.toString(),kangStr)){
            float v = temp - 5f;
            if (v<=0){
                stringBuilder.append(toNumAcAmb).append(temp-5f);
            }else {
                stringBuilder.append(toNumAcAmbF).append(temp-5f);
            }
            stringBuilder.append(toNumAcAmbDf).append(temp+plusVal);
        }else {
            stringBuilder.append(toNumAcAmb).append(temp+plusVal);
            stringBuilder.append(toNumAcAmbD).append(temp-5f);
        }
        return stringBuilder;
    }
    private String endSocStr = "  to_number(\"end_bms_soc\")>95";
    public StringBuilder endSocDim(StringBuilder stringBuilder){
        stringBuilder.append(endSocStr);
        return stringBuilder;
    }

    private String orderByStr = " order by ";
    private String orderInnerSoc = " abs(abs(to_number(\"bms_innersoc\"))-";
    private String orderCurr=" abs(abs(to_number(\"bms_battcurr_rm\"))-";
    private String orderSct=" \"startchargetime\" desc limit 100 ";
    private String orderSctObc =" \"startchargetime\" desc limit 1";
    private String orderSctObcV2 =" \"startchargetime\" desc limit 200";
    private String orderSteadySoc="  \"end_bms_soc\" desc,\"startchargetime\" desc , ";
    private String orderAcAmb=" abs(abs(to_number(\"bms_rmc_moduletempmin\"))-";
    private String kuoHu = "),";
    private String kuoHu1 = ")";

    public StringBuilder orderBy1(StringBuilder stringBuilder,Object acambtemp,Object bmsBattcurr){
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        float temp = Math.abs(Float.parseFloat(acambtemp.toString()));
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderCurr).append(curr).append(kuoHu);
        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu1);
        stringBuilder.append(limitStr);
        return stringBuilder;
    }
    public StringBuilder orderBy2(StringBuilder stringBuilder,Object acambtemp,Object bmsBattcurr,Object bmsSoc){
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        float temp = Math.abs(Float.parseFloat(acambtemp.toString()));
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderInnerSoc).append(bmsSoc).append(kuoHu);
        stringBuilder.append(orderSteadySoc);
        stringBuilder.append(orderCurr).append(curr).append(kuoHu);
        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu1);
        stringBuilder.append(limitStr2);
        return stringBuilder;
    }
    public StringBuilder orderBy3(StringBuilder stringBuilder,Object acambtemp,Object bmsBattcurr){
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        float temp = Math.abs(Float.parseFloat(acambtemp.toString()));
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderCurr).append(curr).append(kuoHu);
        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu1);
        stringBuilder.append(limitStr3);
        return stringBuilder;
    }

    public StringBuilder orderByDc(StringBuilder stringBuilder, Object acambtemp, Object bmsBattcurr){
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        float temp = Math.abs(Float.parseFloat(acambtemp.toString()));
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderCurr).append(curr).append(kuoHu1);
//        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu1)
        stringBuilder.append(limitStr3);
        return stringBuilder;
    }
    public StringBuilder orderByDc2(StringBuilder stringBuilder, Object acambtemp, Object bmsBattcurr){
//        float curr = Math.abs(Float.parseFloat(bms_battcurr.toString()))
//        float temp = Math.abs(Float.parseFloat(acambtemp.toString()))
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderSct);
//        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu1)
//        stringBuilder.append(limitStr3)
        return stringBuilder;
    }
    public StringBuilder orderByObc2(StringBuilder stringBuilder, Object acambtemp, Object bmsBattcurr){
//        float curr = Math.abs(Float.parseFloat(bms_battcurr.toString()))
//        float temp = Math.abs(Float.parseFloat(acambtemp.toString()))
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderSctObc);
//        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu1)
//        stringBuilder.append(limitStr3)
        return stringBuilder;
    }
    public StringBuilder orderByObc3(StringBuilder stringBuilder, Object acambtemp, Object bmsBattcurr){
//        float curr = Math.abs(Float.parseFloat(bms_battcurr.toString()))
//        float temp = Math.abs(Float.parseFloat(acambtemp.toString()))
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderSctObcV2);
//        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu1)
//        stringBuilder.append(limitStr3)
        return stringBuilder;
    }
    public StringBuilder orderBy(StringBuilder stringBuilder,Object acambtemp,Object bmsBattcurr,Object bmsSoc){
        float soc = Math.abs(Float.parseFloat(bmsSoc.toString()));
        float curr = Math.abs(Float.parseFloat(bmsBattcurr.toString()));
        float temp = Math.abs(Float.parseFloat(acambtemp.toString()));
        stringBuilder.append(orderByStr);
//        stringBuilder.append(orderCurr).append(curr).append(kuoHu)
//        stringBuilder.append(orderAcAmb).append(temp).append(kuoHu)
        stringBuilder.append(orderInnerSoc).append(soc).append(kuoHu1);
        stringBuilder.append(limitStr);
        return stringBuilder;
    }

    private String orderDesc = " order by \"item_time\" desc ";
    private String limitStr = " limit 1";
    private String limitStr2 = " limit 3";
    private String limitStr3 = " ";
    public StringBuilder orderByEndSoc(StringBuilder stringBuilder){
        stringBuilder.append(orderDesc);
        stringBuilder.append(limitStr);
        return stringBuilder;
    }
}
